
import React from 'react';
import * as THREE from 'three';
import Controller from "../Controller";

/** 设置地板 */
export default function useFloor(controller: Controller) {

    const scene = controller.useMemo(s => s.scene, [])

    React.useEffect(() => {
        if (!scene) return;

        // 创建地面
        controller.run(async s => {
            const geometry = new THREE.PlaneGeometry(100, 100);
            const material = new THREE.MeshPhongMaterial({ color: 0xffffff, side: THREE.DoubleSide });
            s.floor = new THREE.Mesh(geometry, material);
            s.floor.receiveShadow = true;
            s.floor.castShadow = true;
            s.floor.rotation.set(Math.PI / 2, 0, 0);
            s.version++;
            scene.add(s.floor);
        })

        return () => {
            controller.run(async s => {
                if (!s.floor) return;
                scene.remove(s.floor);
                s.floor = undefined;
            })
        }
    }, [controller, scene])
}